package mock;

import com.github.javafaker.Faker;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import java.util.Locale;
import bean.PriceRule;

public class PriceRuleDataGenerator {
    private static final String DB_URL = "jdbc:mysql://192.168.10.102:3306/driver?useSSL=false&requireSSL=true&useUnicode=true&characterEncoding=UTF-8&serverTimezone=UTC";
    private static final String DB_USER = "root";
    private static final String DB_PASSWORD = "000000";
    private static final int BATCH_SIZE = 100;

    public static void main(String[] args) {
        int count = 100; // 默认生成10条数据
        if (args.length >= 1) {
            count = Integer.parseInt(args[0]);
        }
        generateData(count);
    }

    public static void generateData(int count) {
        Faker faker = new Faker(Locale.CHINA);
        List<PriceRule> rules = new ArrayList<>();

        for (long i = 1; i <= count; i++) {
            PriceRule rule = PriceRule.builder()
                    .city_id(String.valueOf(faker.random().nextInt(1, 100)))
                    .car_type(generateRandomCarType(faker))
                    .base_price(String.valueOf(faker.number().randomDouble(2, 10, 20)))
                    .price_per_km(generateRandompk(faker))
                    .price_per_min(generateRandommk(faker))
                    .build();
            rules.add(rule);
        }

        try (Connection conn = DriverManager.getConnection(DB_URL, DB_USER, DB_PASSWORD)) {
            conn.setAutoCommit(false);
            String sql = "INSERT INTO price_rule (" +
                    "city_id, car_type, base_price, price_per_km, price_per_min" +
                    ") VALUES (?, ?, ?, ?, ?)";

            try (PreparedStatement pstmt = conn.prepareStatement(sql)) {
                for (int i = 0; i < rules.size(); i++) {
                    PriceRule rule = rules.get(i);
                    pstmt.setString(1, rule.getCity_id());
                    pstmt.setString(2, rule.getCar_type());
                    pstmt.setString(3, rule.getBase_price());
                    pstmt.setDouble(4, rule.getPrice_per_km());
                    pstmt.setDouble(5, rule.getPrice_per_min());
                    pstmt.addBatch();

                    if ((i + 1) % BATCH_SIZE == 0) {
                        pstmt.executeBatch();
                    }
                }
                pstmt.executeBatch();
                conn.commit();
                System.out.println("成功插入" + count + "条价格规则");
            } catch (SQLException e) {
                conn.rollback();
                e.printStackTrace();
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }

    private static String generateRandomCarType(Faker faker) {
        String[] carTypes = {"SUV", "MPV", "轿车", "面包车", "商务车", "皮卡"};
        return carTypes[faker.random().nextInt(carTypes.length)];
    }
    private static double generateRandompk(Faker faker) {
        return Math.round(faker.number().randomDouble(2,1 ,3))  ;
    }

    private static double generateRandommk(Faker faker) {
        return Math.round(faker.number().randomDouble(2, 1, 2));
    }
}
